*********************************************************************************************
*********************************************************************************************
*																	
*						Analysis					
*																	
*********************************************************************************************
*********************************************************************************************

* Date: 		March 18
* Author: 		Maria Preißinger
* Project: 		Preißinger & Schoen (2018) Lay Theory Paper PAID
* Description: 	This Dofile reproduces all analyses reported in the paper 



*---------------------------------------------------
* Table 1
*------------------------------------------------------


sum item?


est clear
estpost sum item?
esttab using tables.rtf, ///
	cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2)  par(( )))") noobs ///
	collabels("N" "M" "(SD)") mtitle("Active change") nonumbers ///
	compress replace title(Table 1)
	

/* Anteil der Befragten auf Mittelkategorie händisch nachtragen */	
fre item?




*-----------------------------------------
* Table S1
*------------------------------------------


lab var bus "Offer a seat"
lab var drängel "Jump a queue" 


est clear
estpost sum bus drängel
esttab using tables.rtf, ///
	cells("count(fmt(0)) mean(fmt(2)) sd(fmt(2) par(( )))") noobs ///
	collabels("N" "Mean" "(SD)") nonumbers compress append nomtitles ///
	lab title(Table S1)



*-------------------------------------------------------------								
* Table 2 & Table 3
*-------------------------------------------------------------			
				
				
* Modell 1: One-dimensional model without measurement error theory
est clear
sem (Laytheory -> item?), stand							
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estimates store active1

* Model 2: Two-dimensional model
sem (Incremental -> item2 item4 item5) ///
	(Entity -> item1 item3 item6 item7), stand
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estadd lrtest active1
estimates store active2


* Model 3: One-dimensional model with measurement error theory
sem (Laytheory -> item?), ///
	cov(e.item2*e.item4 e.item2*e.item5 e.item4*e.item5) stand
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estadd lrtest active1
estimates store active3


* Table 2: Fit Indecies
esttab active? using tables.rtf, ///
	cells(b_std(fmt(2)) & _star) ///
	stats(N chi2 df pchi2 lrtest_chi2 lrtest_p rmsea cfi srmr aic, fmt(0 0 0 2 0 2 2 2 2 0)) ///
	keep(cov(Incremental,Entity)) mtitle("A)single factor" "B)two factors" "C)error model") ///
	nonumbers nolz append title("Table 2: CFA Model Comparison") compress  ///
	coeflabel(cov(Incremental,Entity) "latent corr.")  ///
	collabel(none) eqlabel(none) varwidth(12)
	
	
	
* Table 3: Factor loadings of correlations error model
esttab active3 using tables.rtf, ///
	cells(b_std(fmt(2)) & _star) drop(item?:_cons) ///
	nolz eqlabel(none) ///
	coeflabel(item1:Laytheory 	"item1" ///
	item2:Laytheory				"item2" ///
	item3:Laytheory 			"item3"  ///
	item4:Laytheory 			"item4"  ///
	item5:Laytheory 			"item5" ///
	item6:Laytheory 			"item6"  ///
	item7:Laytheory 			"item7" ///
	var(e.item1):_cons 			"error.item1" ///
	var(e.item2):_cons 			"error.item2" ///
	var(e.item3):_cons 			"error.item3" ///
	var(e.item4):_cons 			"error.item4" ///
	var(e.item5):_cons 			"error.item5" ///
	var(e.item6):_cons 			"error.item6" ///
	var(e.item7):_cons 			"error.item7" ///
	var(Laytheory):_cons 		"Lay Theory" ///
	cov(e.item2,e.item4):_cons	"errors item2&item4" ///
	cov(e.item2,e.item5):_cons "errors item2&item5" ///
	cov(e.item4,e.item5):_cons "errors item4&item5") ///
	collabel(none) nonumbers compress ///
	varwidth(30) append ///
	title("Table 3: CFA Error model")
	

	
*----------------------------------------
* Table 4
*-----------------------------------------	
	
	
est clear

/* Basismodell: gleiche Faktorladungen, gleiche Variaznzen der Faktoren,
	und gleiceh Messfehlervarianzen über die Zeit 
	gleiche Varainzen der Faktoren geht gar nicht, weil Lay2 ja
	eine latent dependent variabel ist
	
	also Basismodell: gleiche Faktirladungen, gleiche Messfehlervarianzen*/
	


* Basismodell	
sem (Lay1 -> item1@1 item2@b item3@c item4@d item5@e item6@f item7@g) ///
	(Lay2 -> item1w2@1 item2w2@b item3w2@c item4w2@d item5w2@e item6w2@f item7w2@g) ///
	(Lay1 -> Lay2), ///
	cov(e.item1@h e.item2@i e.item3@j e.item4@k e.item5@l e.item6@m e.item7@n ///
	e.item1w2@h e.item2w2@i e.item3w2@j e.item4w2@k e.item5w2@l e.item6w2@m ///
	e.item7w2@n e.item2*e.item4 e.item2*e.item5 e.item4*e.item5 ///
	e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estimates store fullrestrict

* Restriktion Messfehler gleich auflösen, zu Messfehlerkorrelationen ab-
* schwächen

sem (Lay1 -> item1@1 item2@b item3@c item4@d item5@e item6@f item7@g) ///
	(Lay2 -> item1w2@1 item2w2@b item3w2@c item4w2@d item5w2@e item6w2@f item7w2@g) ///
	(Lay1 -> Lay2), ///
	cov(e.item1*e.item1w2 e.item2*e.item2w2 e.item3*e.item3w2 ///
	e.item4*e.item4w2 e.item5*e.item5w2 e.item6*e.item6w2 e.item7*e.item7w2 ///
	e.item2*e.item4 e.item2*e.item5 e.item4*e.item5 ///
	e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estadd lrtest fullrestrict 
estimates store letgosameerror
		


* Restriktion gleiche Faktorladungen aufheben

sem (Lay1 -> item1 item2 item3 item4 item5 item6 item7) ///
	(Lay2 -> item1w2 item2w2 item3w2 item4w2 item5w2 item6w2 item7w2) ///
	(Lay1 -> Lay2), ///
	cov(e.item1*e.item1w2 e.item2*e.item2w2 e.item3*e.item3w2 ///
	e.item4*e.item4w2 e.item5*e.item5w2 e.item6*e.item6w2 e.item7*e.item7w2 ///
	e.item2*e.item4 e.item2*e.item5 e.item4*e.item5 ///
	e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar df = r(df_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar pclose = r(pclose)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estadd scalar pchi2 = r(p_ms)
estadd scalar aic = r(aic)
estadd lrtest letgosameerror 
estimates store letgosameloading


esttab fullrestrict letgosameerror letgosameloading ///
	using tables.rtf, ///
	stats(N chi2 df lrtest_chi2 lrtest_df lrtest_p rmsea pclose cfi srmr pchi2 aic, ///
	fmt(0 0 0 0 0 2 2 3 2 2 2 2 3)) ///
	drop(*) mtitles("same loadings, same errors" ///
	"same loadings, autocorrelated errors" ///
	"free loadings, autocorrelated errors") ///
	nolegend not append nolz ///
	title("Table 4: Invariant measurement model? Model comparison")
		

est clear
sem (Lay1 -> item1@1 item2@b item3@c item4@d item5@e item6@f item7@g) ///
	(Lay2 -> item1w2@1 item2w2@b item3w2@c item4w2@d item5w2@e item6w2@f item7w2@g) ///
	(Lay1 -> Lay2), stand ///
	cov(e.item1*e.item1w2 e.item2*e.item2w2 e.item3*e.item3w2 ///
	e.item4*e.item4w2 e.item5*e.item5w2 e.item6*e.item6w2 e.item7*e.item7w2 ///
	e.item2*e.item4 e.item2*e.item5 e.item4*e.item5 ///
	e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)
/* Stabilitätskoeffizienten im Text nennen, keine Tabelle */


*------------------------------------------------------------------
* Table 5
*------------------------------------------------------------------

est clear

local messm  `" "(Laytheory -> item?, cov(e.item2*e.item4 e.item2*e.item5 e.item4*e.item5))," "' 

local i = 0
	
foreach m of local messm {
	local i = `i'+1
	sem (Attribution -> drängel@1) (Laytheory -> Attribution) ///
		`m' stand cov(e.drängel@0)
	estat gof, stats(all)  
	estadd scalar chi2 = r(chi2_ms)
	estadd scalar pchi2 = r(p_ms)
	estadd scalar rmsea = r(rmsea)
	estadd scalar df = r(df_ms)
	estadd scalar cfi = r(cfi)
	estadd scalar srmr = r(srmr)
	estat eqgof
	mat A = r(eqfit)
	estadd scalar r2 = A[ `= rowsof(A)', 4]


	ereturn list
	mat list e(b_std) 
	mat b = e(b_std)
	mat b = b[1, 1..1]


	mat list e(V_std)  
	mat dia = vecdiag(e(V_std))  //Varianzen auf der Diagonalen rausgreifen, Zeilenvektor
	mat dia = dia[1,1..1]		//die ersten drei Splaten rausgreifen, den Rest brauchen wir nicht

	mat se = vecdiag(cholesky(diag(dia))) //"Wurzel" ausrechnen, Choleskyzerlegung
	estadd mat low = (b - 1.96*se)
	estadd mat upp = (b + 1.96*se)

	estimates store drängel`i'


	sem (Attribution -> bus@1) (Laytheory -> Attribution) ///
		 `m' stand cov(e.bus@0)
	estat gof, stats(all)  
	estadd scalar chi2 = r(chi2_ms)
	estadd scalar pchi2 = r(p_ms)
	estadd scalar rmsea = r(rmsea)
	estadd scalar df = r(df_ms)
	estadd scalar cfi = r(cfi)
	estadd scalar srmr = r(srmr)
	estat eqgof
	mat A = r(eqfit)
	estadd scalar r2 = A[ `= rowsof(A)', 4]

	ereturn list
	mat list e(b_std) 
	mat b = e(b_std)
	mat b = b[1, 1..1]


	mat list e(V_std)  
	mat dia = vecdiag(e(V_std))  //Varianzen auf der Diagonalen rausgreifen, Zeilenvektor
	mat dia = dia[1,1..1]		//die ersten drei Splaten rausgreifen, den Rest brauchen wir nicht

	mat se = vecdiag(cholesky(diag(dia))) //"Wurzel" ausrechnen, Choleskyzerlegung
	estadd mat low = (b - 1.96*se)
	estadd mat upp = (b + 1.96*se)

	estimates store bus`i'
}

sem (Attribution -> drängelw2@1) (Laytheory -> Attribution) ///
	(Laytheory -> item?w2, cov(e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)), ///
	stand cov(e.drängelw2@0)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar pchi2 = r(p_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar df = r(df_ms)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estat eqgof
mat A = r(eqfit)
estadd scalar r2 = A[ `= rowsof(A)', 4]


ereturn list
mat list e(b_std) 
mat b = e(b_std)
mat b = b[1, 1..1]


mat list e(V_std)  
mat dia = vecdiag(e(V_std))  //Varianzen auf der Diagonalen rausgreifen, Zeilenvektor
mat dia = dia[1,1..1]		//die ersten drei Splaten rausgreifen, den Rest brauchen wir nicht

mat se = vecdiag(cholesky(diag(dia))) //"Wurzel" ausrechnen, Choleskyzerlegung
estadd mat low = (b - 1.96*se)
estadd mat upp = (b + 1.96*se)

estimates store drängel2


sem (Attribution -> busw2@1) (Laytheory -> Attribution) ///
	 (Laytheory -> item?w2, cov(e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)), ///
	 stand cov(e.busw2@0)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar pchi2 = r(p_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar df = r(df_ms)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estat eqgof
mat A = r(eqfit)
estadd scalar r2 = A[ `= rowsof(A)', 4]

ereturn list
mat list e(b_std) 
mat b = e(b_std)
mat b = b[1, 1..1]


mat list e(V_std)  
mat dia = vecdiag(e(V_std))  //Varianzen auf der Diagonalen rausgreifen, Zeilenvektor
mat dia = dia[1,1..1]		//die ersten drei Splaten rausgreifen, den Rest brauchen wir nicht

mat se = vecdiag(cholesky(diag(dia))) //"Wurzel" ausrechnen, Choleskyzerlegung
estadd mat low = (b - 1.96*se)
estadd mat upp = (b + 1.96*se)

estimates store bus2

esttab drängel1 bus1 drängel2 bus2 using tables.rtf,  ///
	cells(b_std(fmt(2))&_star low(par([ , ]))&upp(par("" "]"))) ///
	stats(N chi2 df r2 pchi2 rmsea cfi srmr, fmt(0 0 0 2 2 2 2 2)) ///
	append nonumbers compress ///
	mtitle("Jump a queue" "Offer a seat" "Jump a queue" "Offer a seat" ) ///
	keep(Attribution:*) ///
	coeflabel(Attribution:Laytheory "Lay Theory") ///
	eqlabels(none) collabels(none) nolz title(Table 5)
		
	

*------------------------------------------------------------------------------------
* Table 6
*--------------------------------------------------------------------------------------	

estimates clear

sem (Laytheory -> item?, cov(e.item2*e.item4 e.item2*e.item5 e.item4*e.item5)) ///
	(Needcog -> needcog1 needcog2 needcog3) ///
	(Needeval -> needeval1 needeval2 needeval3 needeval4) ///
	(Age -> alter@1) (Loweducation -> loweduc@1) ///
	(Higheducation -> higheduc@1) (Frau -> frau@1), ///
	stand cov(e.alter@0 e.loweduc@0  e.higheduc@0  e.frau@0)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar pchi2 = r(p_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar df = r(df_ms)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estimates store corractive		
	

esttab corr* using tables.rtf, ///
	cells(b_std(fmt(2)) & _star)  ///
	labcol2(- + 0 0 0 0, title(Hypothesis)) ///
	stats(N chi2 df pchi2 rmsea cfi srmr, fmt(0 0 0 2 2 2 2)) ///
	nolz append nonumbers mtitle  keep(cov(Lay*)) ///
	coeflabel(cov(Laytheory,Needcog):_cons "Need for Cognition" ///
	cov(Laytheory,Needeval):_cons "Need to Evaluate" ///
	cov(Laytheory,Age):_cons "Age" ///
	cov(Laytheory,Loweducation):_cons "Low Education" ///
	cov(Laytheory,Higheducation):_cons "High Education" ///
	cov(Laytheory,Frau):_cons "Female") ///
	collabels(none) title("Table 6: Correlation with validation criteria")  ///
	varwidth(18) eqlabels(none) compress ///
	note("Structural Equation Model. Only correlations between lay theory and validation criteria are reported. Standardized results.")

	
	
sem (Incremental -> item2 item4 item5) ///
	(Entity -> item1 item3 item6 item7) ///
	(Needcog -> needcog1 needcog2 needcog3) ///
	(Needeval -> needeval1 needeval2 needeval3 needeval4) ///
	(Age -> alter@1) (Loweducation -> loweduc@1) ///
	(Higheducation -> higheduc@1) (Frau -> frau@1), ///
	stand cov(e.alter@0 e.loweduc@0  e.higheduc@0  e.frau@0)
estat gof, stats(all)  
estadd scalar chi2 = r(chi2_ms)
estadd scalar pchi2 = r(p_ms)
estadd scalar rmsea = r(rmsea)
estadd scalar df = r(df_ms)
estadd scalar cfi = r(cfi)
estadd scalar srmr = r(srmr)
estimates store two	
	

esttab corr* two two /*using tables.rtf*/, ///
	cells(b_std(fmt(2)) & _star)  ///
	labcol2(- + 0 0 0 0, title(Hypothesis)) ///
	stats(N chi2 df pchi2 rmsea cfi srmr, fmt(0 0 0 2 2 2 2)) ///
	nolz append nonumbers mtitle  keep(cov(Lay*) cov(Incre*)) varwidth(40) ///
	rename(cov(Incremental,Needcog):_cons cov(Laytheory,Needcog):_cons)
	
	///
	cov(Incremental,Needeval):_cons cov(Laytheory,Needeval):_cons ///
	cov(Incremental,Age):_cons cov(Laytheory,Age):_cons ///
	cov(Incremental,Loweducation):_cons cov(Laytheory,Loweducation):_cons ///
	cov(Incremental,Higheducation):_cons cov(Laytheory,Higheducation):_cons ///
	cov(Incremental,Frau):_cons cov(Laytheory,Frau):_cons) ///
	coeflabel(cov(Laytheory,Needcog):_cons "Need for Cognition" ///
	cov(Laytheory,Needeval):_cons "Need to Evaluate" ///
	cov(Laytheory,Age):_cons "Age" ///
	cov(Laytheory,Loweducation):_cons "Low Education" ///
	cov(Laytheory,Higheducation):_cons "High Education" ///
	cov(Laytheory,Frau):_cons "Female") ///
	collabels(none) title("Table 6: Correlation with validation criteria")  ///
	varwidth(40) eqlabels(none) compress ///
	note("Structural Equation Model. Only correlations between lay theory and validation criteria are reported. Standardized results.")
	
	
	
*--------------------------------------------
* Table S2
*---------------------------------------------	
	
est clear

foreach num of numlist 1/7 {
	eststo: estpost corr laikon`num' drängel bus 
}


esttab using tables.rtf, ///
	append compress cells(b(fmt(2))&_star count(fmt(0) par(( )))) ///
	nolz noobs nonumbers mtitles(V1 V2 V3 V4 V5 V6 V7) ///
	collabels(none) title("Table S2 W1") label ///
	note("Entries are Pearson's r. Entries in parantheses are numbers of observations. * p <.05, ** p<.01, *** p<.001")


est clear


lab var drängelw2 "Jump a queue" 
lab var busw2 "Offer a seat"

foreach num of numlist 1/7 {
eststo: estpost corr laikon`num'w2 drängelw2 busw2
}

esttab using tables.rtf, ///
	append compress cells(b(fmt(2))&_star count(fmt(0) par(( )))) ///
	nolz noobs nonumbers mtitles(V1 V2 V3 V4 V5 V6 V7) ///
	collabels(none) title("Table S2 W2") label ///
	note("Entries are Pearson's r. Entries in parantheses are numbers of observations. * p <.05, ** p<.01, *** p<.001")
	
	
sem (Lay1 -> item1@1 item2@b item3@c item4@d item5@e item6@f item7@g) ///
	(Lay2 -> item1w2@1 item2w2@b item3w2@c item4w2@d item5w2@e item6w2@f item7w2@g) ///
	(Lay1 -> Lay2), stand ///
	cov(e.item1*e.item1w2 e.item2*e.item2w2 e.item3*e.item3w2 ///
	e.item4*e.item4w2 e.item5*e.item5w2 e.item6*e.item6w2 e.item7*e.item7w2 ///
	e.item2*e.item4 e.item2*e.item5 e.item4*e.item5 ///
	e.item2w2*e.item4w2 e.item2w2*e.item5w2 e.item4w2*e.item5w2)
	

predict Lay1 if e(sample), latent(Lay1) 
predict Lay2 if e(sample), latent(Lay2)

sum Lay1, detail 

gen lay1extrem = 1 if Lay1>=r(p75) & Lay1!=. 
replace lay1extrem = 0 if Lay1<=r(p25)

sum Lay2, detail

gen lay2extrem = 1 if Lay2>=r(p75) & Lay2!=. 
replace lay2extrem = 0 if Lay2<=r(p25)
	
tab lay1extrem lay2extrem if Lay1!=. & Lay2!=., m row	
//nur 30% des Panelsamples gehören zu beiden Zeitpunkten 
//zu den oberen oder zu den unteren 25% der jeweiligen Verteilung

gen 	stableextrem = 0 if lay1extrem==0 & lay2extrem==0
replace	stableextrem = 1 if lay1extrem==1 & lay2extrem==1

est clear
eststo: estpost corr stableextrem drängel bus
eststo: estpost corr stableextrem drängelw2 busw2 

esttab using tables.rtf, ///
	append compress cells(b(fmt(2))&_star count(fmt(0) par(( )))) ///
	nolz noobs nonumbers mtitles("Wave 1" "Wave 2") ///
	collabels(none) title("Table S2 Only cases with stable and extreme lay theories") label ///
	rename(drängelw2 drängel busw2 bus gewalt gewaltw2 alos alosw2) ///
	note("Entries are Pearson's r. Entries in parantheses are numbers of observations. * p <.05, ** p<.01, *** p<.001")
	
	
drop Lay2 Lay1	
drop lay?extrem	stableextrem
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
